package com.digiwin.escloud.test.core.common;

import com.digiwin.escloud.test.core.entity.TestCaseEntity;
import org.apache.commons.collections.CollectionUtils;
import org.testng.annotations.DataProvider;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author yanggld
 * @Date 2020/5/13-17:17
 */
public class CommonDataProvider {

	@DataProvider(name = "dataProvider")
	protected Object[][] dataProvider(Method method) {
		List<Object[]> objectList = new ArrayList<>();
		try {
			List<TestCaseEntity> testCaseEntities = TestInitializationContext.testCaseEntityList;
			if (CollectionUtils.isEmpty(testCaseEntities)) {
				return new Object[][]{};
			}
			for (int i = 0; i < testCaseEntities.size(); i++) {
				TestCaseEntity testCaseEntity = testCaseEntities.get(i);
				String name = testCaseEntity.getName();
				if (method.getName().equals(name)) {
					Object[] obj = {testCaseEntity};
					objectList.add(obj);
				}
			}
			if (CollectionUtils.isNotEmpty(objectList)) {
				Object[][] objs = new Object[objectList.size()][];
				for (int i = 0; i < objectList.size(); i++) {
					objs[i] = objectList.get(i);
				}
				return objs;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Object[][]{};
	}
}
